﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using DAL = AccessDAL;
using CommonFunc;

namespace BLL
{
    public class INewsClass
    {
        DAL.INewsClassTableAdapters.INewsClassTableAdapter DINC =
            new DAL.INewsClassTableAdapters.INewsClassTableAdapter();
        DAL.INewsTableAdapters.INewsTableAdapter DIN =
            new DAL.INewsTableAdapters.INewsTableAdapter();

        MODEL.INewsClass _MINC = new MODEL.INewsClass();

        #region GetDataByID
        /// <summary>
        /// GetDataByID
        /// </summary>
        /// <param name="MINC"></param>
        /// <returns></returns>
        public MODEL.INewsClass GetDataByID(MODEL.INewsClass MINC)
        {
            try
            {
                DataTable DT = DINC.GetDataByID(MINC.INC_ID);

                _MINC.INC_ClassCode = DT.Rows[0]["INC_ClassCode"].ToString();
                _MINC.INC_ID = Convert.ToInt32(DT.Rows[0]["INC_ID"]);
                _MINC.INC_IsLock = Convert.ToBoolean(DT.Rows[0]["INC_IsLock"]);
                _MINC.INC_Level = Convert.ToInt32(DT.Rows[0]["INC_Level"]);
                _MINC.INC_ParentID = Convert.ToInt32(DT.Rows[0]["INC_ParentID"]);
                _MINC.INC_Thumbnail = DT.Rows[0]["INC_Thumbnail"].ToString();
                _MINC.INC_Title = DT.Rows[0]["INC_Title"].ToString();

                return _MINC;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 添加图文类别
        /// <summary>
        /// 添加图文类别
        /// </summary>
        /// <param name="MINC"></param>
        public void Add(MODEL.INewsClass MINC)
        {
            try
            {
                string classcode;
                int Level;

                DataTable DT_Title_ParentID = DINC.GetDataByTitle_ParentID(MINC.INC_Title, MINC.INC_ID);
                if (DT_Title_ParentID.Rows.Count != 0)
                {
                    throw new Exception("此分类下，此类别名称已被占用");
                }

                DataTable DT_ParentID = DINC.GetDataByParentID(MINC.INC_ID);
                if (DT_ParentID.Rows.Count == 0)
                {
                    _MINC = GetDataByID(MINC);

                    classcode = ClassCode.CreateChildClassCode(_MINC.INC_ClassCode);
                    Level = _MINC.INC_Level + 1;

                }
                else
                {
                    string OldClassCode = DT_ParentID.Rows[0]["INC_ClassCode"].ToString();
                    Level = Convert.ToInt32(DT_ParentID.Rows[0]["INC_Level"]);
                    classcode = ClassCode.CreateNextClassCode(OldClassCode, Level.ToString());
                }

                DINC.Add(MINC.INC_Title, MINC.INC_ID, classcode, Level,
                    MINC.INC_Thumbnail, MINC.INC_IsLock);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 修改图文类别
        /// <summary>
        /// 修改图文类别
        /// </summary>
        /// <param name="MINC"></param>
        public void Mod(MODEL.INewsClass MINC)
        {
            try
            {
                DINC.Mod(MINC.INC_Title, MINC.INC_ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion


        #region 删除图文类别
        /// <summary>
        /// 删除图文类别
        /// </summary>
        /// <param name="MINC"></param>
        public void Del(MODEL.INewsClass MINC)
        {
            try
            {
                DataTable DT_Del;
                DT_Del = DINC.GetDataByParentID(MINC.INC_ID);
                if (DT_Del.Rows.Count != 0)
                {
                    throw new Exception("此分类下有子分类，不可删除，请清空子分类后重试");
                }

                DT_Del = DIN.GetDataByClassID(MINC.INC_ID);
                if (DT_Del.Rows.Count != 0)
                {
                    throw new Exception("此分类下有图文或者回收站内有属于此类别图文，不可删除，请清空分类下图文后重试");
                }

                _MINC = GetDataByID(MINC);
                DT_Del = DINC.GetDataByParentID(0);
                if (DT_Del.Rows.Count == 1 && _MINC.INC_Level == 1)
                {
                    throw new Exception("根分类不得为零项");
                }

                DINC.Del(MINC.INC_ID);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        #endregion
    }
}
